package Admin;

import java.awt.EventQueue;
import java.awt.Rectangle;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;

import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JTextArea;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

import com.example.finalproj.ChatData;

public class Chat_Internal_Frame extends JInternalFrame {
	
//	Management mana;
	String login_id="";
	String irum="";
	HashMap<String, ServerThread> user;
	HashMap<String, Chatting> talk;
	HashMap<String, Chat_Internal_Frame> design;
	
	JTextArea ta_log;
//	HashMap<String, Chat_Internal_Frame> chat_list;
	
	JDesktopPane dp;

	
	public String getIrum() {
		return irum;
	}

	public void setIrum(String irum) {
		this.irum = irum;
	}

	public String getLogin_id() {
		return login_id;
	}

	public void setLogin_id(String login_id) {
		this.login_id = login_id;
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Chat_Internal_Frame frame = new Chat_Internal_Frame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Chat_Internal_Frame() {
		setResizable(true);
		setMaximizable(true);
		setIconifiable(true);
		setClosable(true);
		setDefaultCloseOperation(JInternalFrame.HIDE_ON_CLOSE);
		
		setBounds(100, 100, 450, 300);
		talk = new HashMap<String, Chatting>();
		dp = new JDesktopPane();
//		Chatting cc = new Chatting();
//		dp.add(cc);
//		cc.setVisible(true);

	}
	public Chat_Internal_Frame(HashMap<String, ServerThread> user, JTextArea ta_log, HashMap<String, Chat_Internal_Frame> design)
	{
		this();
		addInternalFrameListener(new InternalFrameAdapter() {
			@Override
			public void internalFrameOpened(InternalFrameEvent arg0) {
					getContentPane().add(new ChatLogin(Chat_Internal_Frame.this));
			}
		});
		this.ta_log = ta_log;
		this.user = user;
		this.design = design;
	}
	public Chat_Internal_Frame(HashMap<String, ServerThread> user, JTextArea ta_log, String toid)
	{
		this();
		addInternalFrameListener(new InternalFrameAdapter() {
			@Override
			public void internalFrameOpened(InternalFrameEvent arg0) {
					getContentPane().add(new ChatLogin(Chat_Internal_Frame.this, toid));
			}
		});
		this.ta_log = ta_log;
		this.user = user;
		setTitle(toid + " 디자이너");
	}
	
	public void logon()
	{
		getContentPane().removeAll();
		getContentPane().add(dp);
		getContentPane().invalidate();
		dp.invalidate();
		Rectangle rt = this.getBounds();
		rt.x += 1;
		rt.y += 1;
		this.setBounds(rt);
	}
	public void receiveMsg(ChatData cd)
	{
		Iterator<String> iter = talk.keySet().iterator();
		boolean have = false;
		String who;
		if(cd.getGrade().equals("3"))
		{
			who = cd.getFromid();
		}
		else
		{
			who = cd.getToid();
		}
		
		while(iter.hasNext())
		{
			if(iter.next().equals(who))
			{
				have = true;
				break;
			}
		}
		if(!have)
		{
			Chatting ting = new Chatting(who, user, ta_log);
			talk.put(who, ting);
			dp.add(ting);
			ting.setVisible(true);
			Rectangle rec = ting.getBounds();
			Random r = new Random(10);
			System.out.println(r.nextInt(10));
			rec.x += 10;
			rec.y += 10;
			ting.setBounds(rec);
		}
		switch(cd.getAction())
		{
		case 2:
			talk.get(who).appMsg(cd);
			talk.get(who).setVisible(true);
			this.setVisible(true);
			break;
		case 3:
			talk.get(who).logoutMsg(cd);
			talk.get(who).setVisible(true);
			break;
		}
		
	}
	public void designer()
	{
		design.put(login_id, this);
		System.out.println("design 맵에 들어갔음");
		this.setTitle(login_id+" 디자이너");
		System.out.println("타이틀 셋함");
	}
}
